<?php 
header("Content-type: text/html; charset=utf-8;");
require '../../../ppf/fun.php';
session_start();  
/*上传缩略图*/
$result=array();
if(!isset($_SESSION['uid'])){
	$result[]=array("ret"=>"err","des"=>"用户未登录");
	echo json_encode($result);
    exit;
}
$dir_root=$_SERVER['DOCUMENT_ROOT'];
$dir='/upload/email_attachment/'.date("Y-m",time()).'/';
if(!is_dir($dir_root.$dir)){
	$re=mkdir($dir_root.$dir,0777,true);
	if(!$re){
		$result[]=array("ret"=>"err","des"=>"没有权限创建目录");
		echo json_encode($result);
	    exit;
	}
}	
$upload_attachment=$_FILES['upload_attachment'];
foreach($upload_attachment['name'] as $k=>$fname) 
{
	$fname=htmlspecialchars($fname,ENT_QUOTES);   
	if ($upload_attachment["error"][$k] > 0){
		$result[]=array("ret"=>"err","des"=>"文件  ".$fname." 上传时出错");
		continue;
	}
	$fsize=$upload_attachment["size"][$k];
	$ftmp_name=$upload_attachment["tmp_name"][$k];
	$ftype=$upload_attachment["type"][$k];
	$fext=".".strtolower(pathinfo($fname, PATHINFO_EXTENSION));
	if(strpos(" .php.js.html.htm.shtml.shtm.css.exe.dll.bat",$fext)){
		$result[]=array("ret"=>"err","des"=>"禁止上传的文件类型 ".$fname);
		continue;
	}
	$microtime=explode(" ",microtime());
    $nname=$microtime[1]+$microtime[0];
	
	//保存文件 
	if (move_uploaded_file($ftmp_name, $dir_root.$dir.$nname.$fext)){
		$result[]=array("ret"=>"ok","des"=>"文件 ".$fname." 上传成功","attachment"=>$nname,"attachment_name"=>$fname,"save_path"=>$dir.$nname.$fext);	
	}                     
}
echo json_encode($result);
exit;
function mkThumbnail($src, $width = null, $height = null, $filename = null) {  
    if (!isset($width) && !isset($height))  
        return false;  
    if (isset($width) && $width <= 0)  
        return false;  
    if (isset($height) && $height <= 0)  
        return false;  
  
    $size = getimagesize($src);  
    if (!$size)  
        return false;  
  
    list($src_w, $src_h, $src_type) = $size;  
    $src_mime = $size['mime'];  
    switch($src_type) {  
        case 1 :  
            $img_type = 'gif';  
            break;  
        case 2 :  
            $img_type = 'jpeg';  
            break;  
        case 3 :  
            $img_type = 'png';  
            break;  
        case 15 :  
            $img_type = 'wbmp';  
            break;  
        default :  
            return false;  
    }  
  
    if (!isset($width))  
        $width = $src_w * ($height / $src_h);  
    if (!isset($height))  
        $height = $src_h * ($width / $src_w);  
  
    $imagecreatefunc = 'imagecreatefrom' . $img_type;  
    $src_img = $imagecreatefunc($src);  
    $dest_img = imagecreatetruecolor($width, $height);  
    imagecopyresampled($dest_img, $src_img, 0, 0, 0, 0, $width, $height, $src_w, $src_h);  
  
    $imagefunc = 'image' . $img_type;  
    if ($filename) {  
        $imagefunc($dest_img, $filename);  
    } else {  
        header('Content-Type: ' . $src_mime);  
        $imagefunc($dest_img);  
    }  
    imagedestroy($src_img);  
    imagedestroy($dest_img);  
    return true;  
}  